package com.nuoniu.sibanyun.controller.crm;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.CrmGoods;
import com.nuoniu.sibanyun.entity.crm.dto.CrmGoodsSaveDto;
import com.nuoniu.sibanyun.service.crm.ICrmGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 客户商品表 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2020-07-17
 */
@RestController
@RequestMapping("/member/goods")
@Api(tags = "商品管理")
public class CrmGoodsController extends BaseController {

    @Autowired
    private ICrmGoodsService goodsService;

    /**
     * 获取商品列表
     * @author 小啊刚
     * @date 2020-07-17
     */
    @ApiOperation("获取商品列表")
    @GetMapping("page")
    public HttpResult pageGoods(Page page, CrmGoods goods) {
        return HttpResult.ok(goodsService.pageGoods(page, goods));
    }
    /**
     * 获取话费套餐列表
     * @author 白强
     * @date 2022-05-27
     */
    @ApiOperation("获取话费套餐列表")
    @PostMapping("pageGoodsSetting")
    public HttpResult pageGoodsSetting(@RequestBody Page page) {
        return HttpResult.ok(goodsService.pageGoodsSetting(page));
    }

    /**
     * 保存商品
     * @author 小啊刚
     * @date 2020-07-17
     */
    @ApiOperation("保存商品")
    @PostMapping("save")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult saveGoods(@RequestBody CrmGoodsSaveDto crmGoodsSaveDto) {
        goodsService.saveGoods(crmGoodsSaveDto);
        return HttpResult.ok();
    }

    /**
     * 停用商品
     * @author 小啊刚
     * @date 2020-07-17
     */
    @ApiOperation("停用商品")
    @PostMapping("disable")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult disable(@RequestBody List<Long> ids) {
        for(Long id : ids) {
            UpdateWrapper wrapper = new UpdateWrapper();
            wrapper.eq("id", id);
            wrapper.set("status", 2);
            goodsService.update(wrapper);
        }
        return HttpResult.ok();
    }

    /**
     * 启用商品
     * @author 小啊刚
     * @date 2020-07-17
     */
    @ApiOperation("启用商品")
    @PostMapping("enable")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult enable(@RequestBody List<Long> ids) {
        for(Long id : ids) {
            UpdateWrapper wrapper = new UpdateWrapper();
            wrapper.eq("id", id);
            wrapper.set("status", 1);
            goodsService.update(wrapper);
        }
        return HttpResult.ok();
    }

    @ApiOperation("使用商品")
    @GetMapping("useGoods")
    public HttpResult useGoods(Long id) {
        goodsService.useGoods(id);
        return HttpResult.ok();
    }

    @ApiOperation("商品排序")
    @PostMapping("updateGoodsSort")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult saveSort(@RequestBody List<CrmGoods> goodsList) {
        goodsService.updateBatchById(goodsList);
        return HttpResult.ok();
    }
}
